@echo off

set env=%1%
set projectName=%2%

echo "[Debug:info] env=%env%"
echo "[Debug:info] projectName=%projectName%"

set "healthUrl="
IF "%env%" == "Dev20" (
	IF "%projectName%" == "trunklpg" (
		set "healthUrl=http://test.117915.com/dev.trunklpg/login.aspx"
	)
	IF "%projectName%" == "SaaSAPPlpg" (
		set "healthUrl=http://test.117915.com/dev.SaaSAPPlpg"
	)
	IF "%projectName%" == "DccAdmin" (
		set "healthUrl=http://test.117915.com/DccAdmin/Ajax/DccClient/LoadAllConnections.ashx"
	)
	IF "%projectName%" == "NewWXShoplpg" (
		set "healthUrl=http://test.117915.com/dev.wxshopapi"
	)
)
IF "%env%" == "lufei" (
	IF "%projectName%" == "trunklpg" (
		set "healthUrl=http://192.168.80.1:8012/trunklpg/login.aspx"
	)
	IF "%projectName%" == "SaaSAPPlpg" (
		set "healthUrl=http://192.168.80.1:8012/SaaSAPPlpg"
	)
	IF "%projectName%" == "DccAdmin" (
		set "healthUrl=http://192.168.80.1:8012/DccAdmin/Ajax/DccClient/LoadAllConnections.ashx"
	)
	IF "%projectName%" == "NewWXShoplpg" (
		set "healthUrl=http://192.168.80.1:8012/wxshopapi"
	)
)

IF "%healthUrl%" == "" (
	echo "[Error:info] not exists web path: %healthUrl%"
    GOTO ERROR
)

setlocal enabledelayedexpansion
set /a retry=1,add=1
GOTO CHECKURL


:CHECKURL
echo "[Debug:info] [Round !retry!]start the check health url: %healthUrl%"
for /f %%a in ('powershell -command "& {try { $response = Invoke-WebRequest %healthUrl%;$Response.StatusCode} catch {$_.Exception.Response.StatusCode.Value__;}}"') do (
	set statusCode=%%a
)
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

IF "%statusCode%"=="200" (GOTO OK)^
ELSE IF !retry! LEQ 5 (
	set /a retry+=1
	GOTO CHECKURL
)^
ELSE (GOTO ERROR)

:OK
ECHO command success
EXIT 0

:ERROR
ECHO command failed
EXIT 1